﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using HslCommunication;
using HslCommunication.Core.IMessage;
using HslCommunication.Core.Net;
using HslCommunication.Core;
using HslCommunication.Profinet.Melsec;
using WindowsFormsApp2.PLC;
using System.IO;
using HslCommunication.ModBus;
using WindowsFormsApp2.PLCmodbus;

namespace YJYpublicClass
{
    public class PLC_modbus 
    {
        private ModbusTcpNet Plcnet = new ModbusTcpNet();
        public Boolean IsConnect = false;
        public string plcsavepath = "FX5UPLC.xml";
        public int ReceiveTimeout { set { Plcnet.ReceiveTimeOut = value; } get { return Plcnet.ReceiveTimeOut; } }
        private static object lockobj=new object();
        public string Mes
        {
            get;
            set;
        }
        public JIAYUPLCconfig plcconfig { get; set; } = new JIAYUPLCconfig();

        public Boolean DisConnect()
        {
            OperateResult result = Plcnet.ConnectClose();
            if (result.IsSuccess)
            {
                IsConnect = false;
                return true;
            }
            else
            {
                IsConnect = false;
                return false;
            }
        }
        public Boolean ConnectSrver(string IPadress, string port,byte station)
        {
            Plcnet.Station=station;
            Plcnet.IpAddress = IPadress;
            Plcnet.Port = Convert.ToInt32(port);
            Plcnet.ConnectTimeOut = 1000;
            Plcnet.ReceiveTimeOut = 1000;
            OperateResult result = Plcnet.ConnectServer();
            if (result.IsSuccess)
            {
                IsConnect = true;
                return true;
            }
            else
            {
                IsConnect = false;
                return false;
            }
        }
        public void ReadD(string address, out int value)
        {
            int result = -1;
            try
            {

                if (IsConnect)
                {
                    HslCommunication.OperateResult<Int32> t = Plcnet.ReadInt32(address);
                    if (t.IsSuccess)
                    {
                        result = t.Content;
                        value = result;
                    }
                    else
                    {
                        value = result;
                    }

                }
                else
                {
                    value = result;
                }
            }
            catch (Exception ex)
            {
                value = result;
                Mes = ex.ToString();
            }

        }
        public void ReadStr(string address,ushort length,out string value)
        {
            string result = "-1";
            try
            {

                if (IsConnect)
                {
                    HslCommunication.OperateResult<string> t = Plcnet.ReadString(address,length);
                    if (t.IsSuccess)
                    {
                        result = t.Content;
                        value = result;
                    }
                    else
                    {
                        value = result;
                    }

                }
                else
                {
                    value = result;
                }
            }
            catch (Exception ex)
            {
                value = result;
                Mes = ex.ToString();
            }
        }


        public Boolean WriteD(string address, int value)
        {
            try
            {           
                if (IsConnect)
                {
                    if (Plcnet.Write(address, value).IsSuccess)
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }

                }
                else
                {
                    return false;
                }
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        public void ReadF(string address, out string value)
        {
            string result = "-1";
            try
            {

                if (IsConnect)
                {
                    HslCommunication.OperateResult<float> t = Plcnet.ReadFloat(address);
                    if (t.IsSuccess)
                    {
                        result = t.Content.ToString();
                        value = result;
                    }
                    else
                    {
                        value = result;
                    }

                }
                else
                {
                    value = result;
                }
            }
            catch (Exception ex)
            {
                value = result;
                Mes = ex.ToString();
            }
        }

        public Boolean WriteF(string address, string value)
        {
            try
            {
                float valuet = 0.0f;
                float.TryParse(value, out valuet);
                if (IsConnect)
                {
                    if (Plcnet.Write(address, valuet).IsSuccess)
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }

                }
                else
                {
                    return false;
                }
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        public void ReadDouble(string address, out string value)
        {
            string result = "-1";
            try
            {

                if (IsConnect)
                {
                    HslCommunication.OperateResult<double> t = Plcnet.ReadDouble(address);
                    if (t.IsSuccess)
                    {
                        result = t.Content.ToString();
                        value = result;
                    }
                    else
                    {
                        value = result;
                    }

                }
                else
                {
                    value = result;
                }
            }
            catch (Exception ex)
            {
                value = result;
                Mes = ex.ToString();
            }
        }

        public Boolean WriteDouble(string address, string value)
        {
            try
            {
                double valuet = 0.0;
                double.TryParse(value, out valuet);
                if (IsConnect)
                {
                    if (Plcnet.Write(address, valuet).IsSuccess)
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }

                }
                else
                {
                    return false;
                }
            }
            catch (Exception ex)
            {
                return false;
            }
        }
        public void ReadCoil(string address, out string value)
        {
            string result = "-1";
            try
            {

                if (IsConnect)
                {
                    HslCommunication.OperateResult<Boolean> t = Plcnet.ReadBool(address);
                    if (t.IsSuccess)
                    {
                        result = t.Content.ToString();
                        value = result;
                    }
                    else
                    {
                        value = result;
                    }

                }
                else
                {
                    value = result;
                }
            }
            catch (Exception ex)
            {
                value = result;
                Mes = ex.ToString();
            }
        }

        public Boolean WriteCoil(string address, bool value)
        {
            try
            {

                if (IsConnect)
                {
                    if (Plcnet.Write(address, value).IsSuccess)
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }

                }
                else
                {
                    return false;
                }
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        public bool isConnect()
        {
            return IsConnect;
        }

        public void setConfig()
        {
            Frm_PLCmodbus tempform = new Frm_PLCmodbus(this);
            tempform.Show();
        }

        public void SetRecivetimeout(int time)
        {
            ReceiveTimeout =time;
        }

        //保存配置和加载配置也一并完成
        public ResultFunc Saveconfig()
        {
            try
            {
                if (XmlHelper.SaveXML(plcsavepath, plcconfig) == "")
                {
                    return new ResultFunc(Funstate.OK, "");
                }
                else
                {
                    return new ResultFunc(Funstate.异常, "保存失败");
                }
            }
            catch (Exception ex)
            {

                return new ResultFunc(Funstate.异常, ex.ToString());
            }
           
        }

        public void LoadConfig()
        {
            if (!File.Exists(plcsavepath))
            {
                plcconfig = new JIAYUPLCconfig();
            }
            else
            {
                plcconfig = XmlHelper.LoadXML<JIAYUPLCconfig>(plcsavepath);
            }
        }
        //初始化也一并完成
        public bool Init()
        {
            //先加载配置，在构成连接
            //
            LoadConfig();
            if(ConnectSrver(plcconfig.Ipaddress, plcconfig.port.ToString(),plcconfig.station))
            {
                return true;
            }else
            {
                return false;
            }
        }

    }

}
